#!/bin/bash
#
# This script uses the Debian kernel package tools to re-compile the currenlty
# running kernel with debugging flags enabled and creates a package for it.
#
# Written 2011 by Christian Schneider <chrschn@sec.in.tum.de>
#

# How to name the debug version based on the running kernel?
APPEND=-dbg

if [ -n "$1" ]; then
	VER="$1"
else
	VER=$(uname -r)
fi

MINOR_VER=$(echo $VER | sed 's/^\([0-9]\.[0-9]\+\(\.[0-9]\+\)\?\).*$/\1/')
APPEND_VER=${VER#$MINOR_VER}${APPEND}

# Make sure we can execute apt-get
if ! [ -x "$(which apt-get 2>/dev/null)" ]; then
	echo "This does not seem to be a Debian based distribution!" >&2
	exit 1
fi

# How do we become root?
if [ $(id -u) == 0 ]; then
	SUDO=""
	ROOTCMD=""
else
	echo "You might be prompted for your password to install depencency packages."
	SUDO="sudo"
	ROOTCMD="--rootcmd $SUDO"
fi

# Resolve build dependencies, get source, and prepare Makefile
$SUDO apt-get -y build-dep linux-image-$VER &&
$SUDO apt-get -y install kernel-package &&
apt-get -y source linux-image-$VER &&
cd linux-*${MINOR_VER}* &&
test -r Makefile.orig || mv -v Makefile Makefile.orig &&
sed \
	-e 's@^\(CFLAGS_\(KERNEL\|MODULE\)\s*=\)@\1 -g @' \
	-e 's@^\(AFLAGS_\(KERNEL\|MODULE\)\s*=\)@\1 -gdwarf-2 @' \
        Makefile.orig > Makefile


# Use gcc_pp, if available
EXEC_DIR="$PWD"
for GCC_PP in "$EXEC_DIR/gcc_pp" "$(dirname $0)/gcc_pp" "$(which gcc_pp)" /usr/share/insight/tools/gcc_pp; do
	if [ -x "$GCC_PP" ]; then
		sed -i "s@^\(CC\\s*=\).*gcc\\s*\$@\1 $GCC_PP@" Makefile
		echo "Using $GCC_PP as compiler"
		break
	fi
done

if ! [ -x "$GCC_PP" ]; then
        echo "WARNING: gcc_pp script not found." >&2
fi

# Apply all patches found in base directory
for f in ../*.diff ../*.patch; do
	if [ -r "$f" ]; then
		echo "Applying patch: $f"
		patch -Np0 -i $f || exit 1
	fi
done

# Run parallel jobs when compiling the source
if [ -z "$CONCURRENCY_LEVEL" ]; then
       	export CONCURRENCY_LEVEL=$(( `grep  -c '^processor' /proc/cpuinfo` + 1))
	# Don't set anything for uni-processor systems
	if [ "$CONCURRENCY_LEVEL" = 2 ]; then
		unset CONCURRENCY_LEVEL
	fi
fi

# Compile source and create package
make-kpkg --us --uc $ROOTCMD --append-to-version $APPEND_VER --initrd kernel_image kernel_headers || exit 1

# Revert the compiler to the original one
if [ -x "$GCC_PP" ]; then
	GCC_ORIG=$(grep '^CC' Makefile.orig | sed "s@^CC\\s*=\(.*\)\$@\1@")
	sed -i "s@^\(CC\\s*=\).*\$@\1 $GCC_ORIG@" Makefile
fi

if [ -r vmlinux ]; then
#	# Create archive of pre-processed sources
#	if [ -x "$GCC_PP" ] && [ -d "__PP__" ]; then
#		echo "Generating archive of pre-processed sources"
#		tar -C ../ -cjvf ../linux-src-pp-${VER}${APPEND}.tar.bz2 "$(basename $PWD)/__PP__/" &&
#		echo "Succesfully created archive of pre-processed sources: linux-src-pp-${VER}${APPEND}.tar.bz2" ||
#		echo "Error creating archive of pre-processed sources" >&2
#	fi
	#bzip2 -9 vmlinux -c > ../vmlinux-${VER}${APPEND}.bz2 &&
	#cp -v vmlinux ../vmlinux-${VER}${APPEND} &&
#	echo -e "\nGenerating object dump for vmlinux-${VER}${APPEND}..." &&
#	objdump -W vmlinux 2>/dev/null | bzip2 -z -c --best > ../objdump-${VER}${APPEND}.bz2 &&
	echo "Done." || echo "Error generating objdump."
fi

